// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004-2009 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using SoapTrace.Core.Diagnostics;
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// Holds the configuration for the <see cref="SoapMessageFormatter"/> object.
	/// </summary>
	[ XmlType( "soapmessageformatter.config" ) ]
	public class SoapMessageFormatterConfig
	{
		/// <summary>
		/// The name of the config file that will hold user specific
		/// settings.
		/// </summary>
		public const string UserConfigurationFilename 
			= "SoapMessageFormatter.dll.user.config";
		
		/// <summary>
		/// The name of the config section that this class maps to.  This needs to
		/// be the same as the XmlType attribute name on the 
		/// class.
		/// </summary>
		public const string ConfigSectionName =
			"soapmessageformatter.config";		
		
		/// <summary>
		/// The configured web services.
		/// </summary>
		private WebServiceCollection m_WebServices = new WebServiceCollection( );

		/// <summary>
		/// The configuration filename.
		/// </summary>
		private string m_Filename = String.Empty;

		/// <summary>
		/// Creates a new instance of the <see cref="SoapMessageFormatterConfig"/>
		/// class.
		/// </summary>
		public SoapMessageFormatterConfig( )
		{
		}

		/// <summary>
		/// Loads the configuration file.
		/// </summary>
		/// <param name="filename">The config filename.</param>
		///<returns>A <see cref="SoapMessageFormatterConfig"/> object.</returns>
		public static SoapMessageFormatterConfig Load( string filename )
		{
			StreamReader Reader = new StreamReader( filename, true );
			
			SoapMessageFormatterConfig Config = Load( Reader );
			
			// Save the filename.

			Config.m_Filename = filename;
			
			return Config;
		}
		
		/// <summary>
		/// Loads the configuration file.
		/// </summary>
		/// <returns>A <see cref="SoapMessageFormatterConfig"/> object.</returns>
		public static SoapMessageFormatterConfig Load( )
		{
			SoapMessageFormatterConfig Config = null;
			
			if( UserSettingsExist )
			{
				// Read user settings.
				
				Logger.Log( String.Concat( "SoapMessageFormatterConfig. Reading user's local config settings. '", UserConfigurationFullFilename, "'." ) );
				Config = Load( UserConfigurationFullFilename );
			}
			else
			{
				// User the application's settings.
				
				Logger.Log( "SoapMessageFormatterConfig. No user settings. Reading app.config." );
				
				Config = ( SoapMessageFormatterConfig )ConfigurationManager.GetSection( ConfigSectionName );
				
				if( Config == null )
				{
					Config = new SoapMessageFormatterConfig( );
					
					Logger.Log( "SoapMessageFormatterConfig. No config in app.config." );				
				}
				
				Config.m_Filename = UserConfigurationFullFilename;
			}	
			
			return Config;
		}
		
		/// <summary>
		/// Loads the configuration from a <see cref="TextReader"/>
		/// </summary>
		/// <param name="reader">The <see cref="TextReader"/> to
		/// read the configuration from.</param>
		/// <returns>A <see cref="SoapMessageFormatterConfig"/> object.</returns>
		public static SoapMessageFormatterConfig Load( TextReader reader )
		{
			XmlSerializer Serializer = new XmlSerializer( typeof( SoapMessageFormatterConfig ) );
			SoapMessageFormatterConfig Config = 
				( SoapMessageFormatterConfig )Serializer.Deserialize( reader );
			
			return Config;
		}

		/// <summary>
		/// Saves the configuration file to the same file we loaded
		/// the configuration from.
		/// </summary>
		public void Save( )
		{
			if( m_Filename.Length == 0 )
			{
				throw new ApplicationException( "No configuration filename set." );
			}

			Save( m_Filename );
		}

		/// <summary>
		/// Saves the configuration file.
		/// </summary>
		/// <param name="filename"></param>
		public void Save( string filename )
		{
			StreamWriter Writer = new StreamWriter( filename, false, Encoding.UTF8 );

			XmlSerializer Serializer = new XmlSerializer( typeof( SoapMessageFormatterConfig ) );
			Serializer.Serialize( Writer, this );
			
			Writer.Close( );
		}

		/// <summary>
		/// The web services that are configured for the message formatter.
		/// </summary>
		[ XmlElement( "webservice" ) ]
		public WebServiceCollection WebServices
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_WebServices;
			}
		}
		
		/// <summary>
		/// Gets whether there are any saved user settings.
		/// </summary>
		private static bool UserSettingsExist
		{
			get
			{
				return File.Exists( UserConfigurationFullFilename );
			}
		}
		
		/// <summary>
		/// Gets the user configuration filename including the path.
		/// </summary>
		private static string UserConfigurationFullFilename
		{
			get
			{
				return Path.Combine( Application.LocalUserAppDataPath, UserConfigurationFilename );
			}
		}		
	}
}
